package com.cljh.service.impl;

import com.cljh.bean.Products;
import com.cljh.bean.User;
import com.cljh.bean.UserAddress;
import com.cljh.controller.page.PageBean;
import com.cljh.controller.page.UserPageBean;
import com.cljh.mapper.ProductsMapper;
import com.cljh.mapper.UserAddressMapper;
import com.cljh.mapper.UserMapper;
import com.cljh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    ProductsMapper productsMapper;
    @Autowired
    UserAddressMapper userAddressMapper;


    @Override
    public boolean save(User user) {
        return userMapper.save(user)>0;
    }

    @Override
    public boolean delete(Integer id) {
        productsMapper.deleteByUserId(id);
        userAddressMapper.deleteAdd(id);
        return userMapper.delete(id)>0;
    }

    @Override
    public boolean update(User user) {
        return userMapper.update(user)>0;
    }



    @Override
    public boolean deleteByIds(List<Integer> ids) {
        for (Integer integer:ids) {
            productsMapper.deleteByUserId(integer);
            userAddressMapper.deleteAdd(integer);
        }
        return userMapper.deleteByIds(ids)>0;
    }

    @Override
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<User> userList() {
        return userMapper.userList();
    }

    @Override
    public List<User> userList1() {
        return userMapper.userList1();
    }

    @Override
    public boolean saveAll(List<User> users) {
        return userMapper.saveAll(users)>0;
    }

    @Override
    public PageBean<User> userConditionList(Integer currentPage, Integer pageSize, User user) {
        //计算开始索引
        int begin = (currentPage - 1) * pageSize;

        String name = user.getName();
        if (name != null && name.length() > 0) {
            user.setName("%" + name + "%");
        }

        List<User> users = userMapper.userConditionList(begin, pageSize, user);
        int i = userMapper.selectByCount(user);
        PageBean<User> userPageBean = new PageBean<>();
        userPageBean.setRows(users);
        userPageBean.setTotalCount(i);
        return userPageBean;
    }

    @Override
    public int countUserNum() {
        return userMapper.countUserNum();
    }

    @Override
    public User selectByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    @Override
    public User selectByUsernameAndPwd(User user) {
        return userMapper.selectByUsernameAndPwd(user);
    }

    @Override
    public User selectByEmail(String username,String emailCode) {
        return userMapper.selectByEmail(username,emailCode);
    }

    @Override
    public User selectByEmailCode(String email) {
        return userMapper.selectByEmailCode(email);
    }
}
